package cn.itsource.service.impl;

import cn.itsource.domain.Product;
import cn.itsource.domain.Store;
import cn.itsource.dto.ProductDto;
import cn.itsource.feign.StoreFeign;
import cn.itsource.mapper.ProductMapper;
import cn.itsource.service.IProductService;
import cn.itsource.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private StoreFeign storeFeign;

    @Override
    public JsonResult add(ProductDto productDto) {
        try {
            Product product = new Product();
            product.setName(productDto.getName());
            product.setPrice(productDto.getPrice());
            productMapper.add(product);

            Store store = new Store();
            store.setNumber(productDto.getNumber());
            store.setProductId(product.getId());
            JsonResult result = storeFeign.add(store);
            if (!result.getSuccess()){
                return result;
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"操作失败");
        }

    }
}
